package org.sky.generator.database.analize.impl;

import com.alibaba.druid.pool.DruidPooledConnection;

import org.apache.logging.log4j.Logger;
import org.sky.generator.database.analize.IAnalizeDatabase;
import org.sky.generator.database.connect.DBConnectionPool;
import org.sky.generator.database.model.Column;
import org.sky.generator.database.model.Table;
import org.sky.util.BeanUtils;
import org.sky.util.LogUtils;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author weifx
 * @Title: MysqlAnalizeDatabase
 * @ProjectName springboot-platform
 * @Description: TODO{Mysql数据库分析}
 * @date 2019/2/26 12:26
 * @Version 1.0
 */
public class MysqlAnalizeDatabase implements IAnalizeDatabase {
    Logger logger = LogUtils.getLogger(MysqlAnalizeDatabase.class);
    /**
     * 表查询SQL
     */
    private final static String TABLES_SQL=
            "SELECT TABLE_NAME,TABLE_COMMENT " +
            "  FROM INFORMATION_SCHEMA.TABLES T" +
            " WHERE T.TABLE_NAME= ? " +
            "   AND T.TABLE_SCHEMA= DATABASE() ";
    /**
     * 字段查询SQL
     */
    private final static String COLUMN_SQL=
            "SELECT TABLE_NAME,COLUMN_NAME,DATA_TYPE,COLUMN_NAME,COLUMN_TYPE,COLUMN_COMMENT " +
            "  FROM INFORMATION_SCHEMA.COLUMNS T" +
            " WHERE T.TABLE_NAME= ? " +
            "   AND T.TABLE_SCHEMA= DATABASE() "+
            "ORDER BY T.ORDINAL_POSITION ASC";
    /**
     * 根据表名获取表信息
     * @param tableName
     * @return
     */
    @Override
    public Table getTable(String tableName) {
        DruidPooledConnection conn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        try {
            conn = DBConnectionPool.getInstance().getConnection();
            pst = conn.prepareStatement(TABLES_SQL);
            pst.setString(1,tableName);
            rs = pst.executeQuery();
            List<Map> list = convertList(rs);
            if(null!=list && !list.isEmpty()){
                Map mapField = list.get(0);
                Table table = BeanUtils.invokeBean(mapField,Table.class);
                return table;
            }else{
                logger.info("查询Tables数据为空");
            }
        }catch (Exception e){
            logger.error("查询Tables出错{}",e);
        }finally {
            DBConnectionPool.closeConnect(rs,pst,conn);
        }
        return null;
    }
    /**
     * 根据表名获取字段信息
     * @param tableName
     * @return
     */
    @Override
    public List<Column> getCloumns(String tableName) {
        DruidPooledConnection conn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        try {
            conn = DBConnectionPool.getInstance().getConnection();
            pst = conn.prepareStatement(COLUMN_SQL);
            pst.setString(1,tableName);
            rs = pst.executeQuery();
            List<Map> list = convertList(rs);
            List<Column> columnList = new ArrayList<>();
            if(null!=list && !list.isEmpty()){
                for(Map mapField:list) {
                    Column column = BeanUtils.invokeBean(mapField, Column.class);
                    columnList.add(column);
                }
                return columnList;
            }
        }catch (Exception e){
            logger.error("查询Columns出错{}",e);
        }finally {
            DBConnectionPool.closeConnect(rs,pst,conn);
        }
        return null;
    }
}
